package export

import (
	"github.com/lab-online/internal/shared/valueobject"
	"github.com/lab-online/internal/user/entity"
	"github.com/lab-online/internal/user/interface/export"
	"github.com/lab-online/pkg/util"
)

type User struct {
	repository Repository
}

func New(repository Repository) export.UserExport {
	return &User{repository}
}

func (u *User) GetUserByUsername(username string) (valueobject.UserVO, error) {
	if userEntity, err := u.repository.GetUserByUsername(username); err != nil {
		return nil, err
	} else {
		return entityToVOMapper(userEntity), nil
	}
}

func (u *User) GetManyUsersByUsername(usernames []string) ([]valueobject.UserVO, error) {
	if entities, err := u.repository.GetManyUsersByUsername(usernames); err != nil {
		return nil, err
	} else {
		return util.SliceMap(entities, entityToVOMapper), nil
	}
}

func entityToVOMapper(entity entity.UserEntity) valueobject.UserVO {
	return valueobject.NewUser(
		valueobject.WithUserName(entity.GetName()),
		valueobject.WithUserRole(uint(entity.GetRole())),
		valueobject.WithUserUsername(entity.GetUsername()),
	)
}
